using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp;

namespace CKY.AgentPlatform.Caching
{
    /// <summary>
    /// 缓存管理应用服务接口
    /// </summary>
    [RemoteService(IsEnabled = true)]
    public interface ICachingAppService : IApplicationService
    {
        /// <summary>
        /// 获取缓存值
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>缓存值</returns>
        Task<string> GetAsync(string key);

        /// <summary>
        /// 设置缓存值
        /// </summary>
        /// <param name="input">设置参数</param>
        Task SetAsync(SetCacheInput input);

        /// <summary>
        /// 删除缓存值
        /// </summary>
        /// <param name="key">缓存键</param>
        Task DeleteAsync(string key);

        /// <summary>
        /// 检查缓存是否存在
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>是否存在</returns>
        Task<bool> ExistsAsync(string key);

        /// <summary>
        /// 获取缓存过期时间
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>过期时间</returns>
        Task<System.DateTimeOffset?> GetExpirationAsync(string key);

        /// <summary>
        /// 清空所有缓存
        /// </summary>
        Task ClearAsync();

        /// <summary>
        /// 获取缓存统计信息
        /// </summary>
        /// <returns>统计信息</returns>
        Task<CacheStatisticsDto> GetStatisticsAsync();

        /// <summary>
        /// 批量获取缓存值
        /// </summary>
        /// <param name="keys">缓存键列表</param>
        /// <returns>键值对字典</returns>
        Task<System.Collections.Generic.Dictionary<string, string>> GetManyAsync(string[] keys);

        /// <summary>
        /// 批量设置缓存值
        /// </summary>
        /// <param name="input">批量设置参数</param>
        Task SetManyAsync(SetManyCacheInput input);

        /// <summary>
        /// 批量删除缓存值
        /// </summary>
        /// <param name="keys">缓存键列表</param>
        Task DeleteManyAsync(string[] keys);
    }

    /// <summary>
    /// 设置缓存输入
    /// </summary>
    public class SetCacheInput
    {
        /// <summary>
        /// 缓存键
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 缓存值
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// 过期时间（秒）
        /// </summary>
        public int? ExpirationSeconds { get; set; }
    }

    /// <summary>
    /// 批量设置缓存输入
    /// </summary>
    public class SetManyCacheInput
    {
        /// <summary>
        /// 键值对列表
        /// </summary>
        public System.Collections.Generic.List<CacheKeyValuePair> Items { get; set; } = new();

        /// <summary>
        /// 默认过期时间（秒）
        /// </summary>
        public int? DefaultExpirationSeconds { get; set; }
    }

    /// <summary>
    /// 缓存键值对
    /// </summary>
    public class CacheKeyValuePair
    {
        /// <summary>
        /// 缓存键
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 缓存值
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// 过期时间（秒）
        /// </summary>
        public int? ExpirationSeconds { get; set; }
    }

    /// <summary>
    /// 缓存统计信息
    /// </summary>
    public class CacheStatisticsDto
    {
        /// <summary>
        /// 缓存项总数
        /// </summary>
        public int TotalCount { get; set; }

        /// <summary>
        /// 内存使用量（字节）
        /// </summary>
        public long MemoryUsage { get; set; }

        /// <summary>
        /// 命中次数
        /// </summary>
        public long HitCount { get; set; }

        /// <summary>
        /// 未命中次数
        /// </summary>
        public long MissCount { get; set; }

        /// <summary>
        /// 命中率
        /// </summary>
        public double HitRate { get; set; }

        /// <summary>
        /// 平均响应时间（毫秒）
        /// </summary>
        public double AverageResponseTime { get; set; }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public System.DateTime LastUpdated { get; set; }
    }
}